decisions
There are types of decisions, two-way, many-way, plus the already discussed computed
decisions.
IF statement
The IF statement is the most basic decision mechanism, providing for a simple choice
between two possibilities. In its simplest form, without the ELSE part, the IF
statement executes a block of statements if a variable or expression is TRUE (non-zero),
or skips it if it is FALSE (zero). In its full form, with the ELSE part, the IF
statement executes the first block of statements if a variable or expression is TRUE, or
the second block if it is FALSE.
The following example shows the two kinds of 2-way decisions that can be built with IF
statements.
FUNCTION IfDemo(x, y)
IF (x < y) THEN x = y : RETURN (-1)
IF (x > y) THEN RETURN (0) ELSE y = x : RETURN (+1)
END FUNCTION
In the first IF statement, x=y and RETURN(-1) are executed if (x<y) is TRUE (x is
less than y), otherwise nothing is executed and program execution continues with the next
source line.
In the second IF statement, RETURN(0) is executed if (x>y) is TRUE (x is greater than
y), otherwise y=x : RETURN(+1) executes.
If an executable statement follows THEN on the same source line, a one-line IF statement
is assumed and the end-of-line serves as an implicit END IF.
Otherwise a multi-line block structured IF is assumed and an explicit END IF is required
to end it, as in the following example:
FUNCTION IfDemo(x, y)
'
IF (x < y) THEN
x = y
RETURN (-1)
END IF
'
IF (x > y) THEN
RETURN (0)
ELSE
y = x
RETURN (+1)
END IF
END FUNCTION
Simple IF statements, like IF (x<y) THEN x=y, are normally written on one line. When more than one statement follows THEN, or when an ELSE section follows the THEN section, multiline form is generally more readable and less error prone.